//
//  AccountCollectVC.m
//  ImGuider
//
//  Created by llt on 2017/5/12.
//  Copyright © 2017年 imguider. All rights reserved.
//

#import "AccountCollectVC.h"

#import "GuiderDetailVC.h"
#import "AccountCollectCell.h"

@interface AccountCollectVC ()<UITableViewDataSource, UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *dataSource;

@end

@implementation AccountCollectVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setup];
}

- (void)setup {

    self.title = MYLocalizedString(@"ACCOUNT_FLLOW", nil);
    
    [self configEmptyDataTableView:self.tableView emptyImageName:@"暂无收藏" describe:MYLocalizedString(@"PUBLIC_NODATA_GUIDER", nil) refresh:nil block:nil];
    
    [ActivityIndicatorView showHUDAddedTo:self.view];
    [self loadNewData];
     
    @weakify(self);
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        @strongify(self);
        
        [self loadNewData];
    }];
    
    [self.tableView setThemeStyle:ThemeStyleBlack];
}

- (NSMutableArray *)dataSource {
    
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (void)loadNewData {
    
    @weakify(self);
    
    [NetAPIManager getCollectBlock:^(NSArray<GuiderCardModel *> *guiderArray, NSError *error) {
        
        @strongify(self);
        
        self.dataHasLoad = YES;
        
        [ActivityIndicatorView hideHUDAddedTo:self.view];
        [self.tableView.mj_header endRefreshing];
        if (error) {
            return;
        }
        [self.dataSource removeAllObjects];
        [self.dataSource addObjectsFromArray:guiderArray];
        [self.tableView reloadData];
        
    }];
}

#pragma mark -UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.dataSource.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    AccountCollectCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    GuiderCardModel *guider = self.dataSource[indexPath.section];
    cell.guider = guider;
    cell.delegate = self;
    return cell;
}

#pragma mark -UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    GuiderCardModel *guider = self.dataSource[indexPath.section];
    GuiderDetailVC *detailVC = [[GuiderDetailVC alloc] init];
    
    detailVC.title = MYLocalizedString(@"GUIDELIST_TITLE", nil);
    detailVC.guiderID = guider.guideid;

    [self.navigationController pushViewController:detailVC animated:YES];
}

- (void)collect:(AccountCollectCell *)cell guider:(GuiderCardModel *)guider {
    
    [NetAPIManager collectWithType:CollectTypeGuider Id:guider.guideid block:^(id result, NSError *error) {
        
        if (!error) {
            guider.isfavor = [result boolValue];
            [self.tableView reloadData];
            
            if ([result boolValue]) {
                [IGUtils showToast:MYLocalizedString(@"GUIDELIST_FOLLOW", nil)];
                
            } else{
                [IGUtils showToast:MYLocalizedString(@"GUIDELIST_FOLLOWCANCEL", nil)];
            }
        }
    }];
}

- (void)collectGuiderIndex:(NSInteger)guiderIndex isfavor:(BOOL)isfavor {
    
    GuiderCardModel *guider = self.dataSource[guiderIndex];
    
    guider.isfavor = isfavor;
    [self.tableView reloadData];
}

@end
